### Lab 3 supporting ###

#  generate Leslie matrix from mx,gx

GetLMat <- function(mx,gx)
{
	mat = matrix(0,ncol=length(mx),nrow=length(mx))
	mat[1,] = mx
	
	for (i in 2:length(mx))  # i is row index
	{
		for(j in 1:length(gx))  # j is column index
		{
			if(i==(j+1))
				mat[i,j] = gx[j]
			else
				mat[i,j] = 0
		}
	}
	return(mat)
}


elasticity<-function(x){
#function for calculating elasticities for 
#Leslie/Lefkovitch matrix, x.
Ex<-eigen(x) #eigendecomposition of matrix
rvec<-Ex$vectors[,1] #store dom eigenvect as rvec
lambda<-Ex$values[1] #dominant eigenvalue
tmp1<-eigen(t(x))$vectors[,1] #intermediate calc 1
tmp2<-tmp1%*%t(rvec) #intermediate calc 2
tmp3<-Mod(tmp2/sum(tmp1*rvec)) #intermediate calc 3
tmp4<-tmp3/lambda #intermediate calc 4
elast<-Mod(x*tmp4) #calculate elasticities
return(elast)
}

